/**
 * 项目名：	Rumba Plan
 * 
 * 版权所有(C)，上海海鼎信息工程股份有限公司，2006，所有权利保留。
 * 
 * 文件名：	ViewDomainSystem.java
 * 模块说明：	
 * 修改历史：
 * 2006-6-22 - lxm - 创建。
 */
package com.hd123.rumba.action.domain;

import net.sf.hibernate.HibernateException;

import com.hd123.rumba.cartridge.FVRumbaSystem;
import com.hd123.rumba.domain.CDomain;
import com.hd123.rumba.domain.biz.BDomain;
import com.hd123.rumba.domain.biz.IBDomainMgr;
import com.hd123.rumba.exception.BusinessException;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.sql.SCE;
import com.hd123.rumba.sql.biz.BSQLSubquery;
import com.hd123.rumba.user.CUser;
import com.hd123.rumba.user.PE;

/**
 * 域 | Action | FVRumbaSystem
 * 
 * @author lxm
 * 
 */
public class ViewDomainSystem extends BaseViewDomain implements Action {

  public static final String CAPTION_VIEW = "域维护";

  /** 权限常量 */
  public static final String FUNCV_PREFIX = FVRumbaSystem.FUNCV_PREFIX;
  public static final String PERMISSION_SELF_VIEW = FUNCV_PREFIX + ".本域.查看";
  public static final String PERMISSION_LOWER_VIEW = FUNCV_PREFIX + ".下级域.查看";
  public static final String PERMISSION_LOWER_RECYCLE_VIEW = FUNCV_PREFIX + ".下级域.回收站.查看";

  public String execute() throws Exception {
    // TODO Auto-generated method stub
    String url = ActionConstants.NOTIMPLEMENTED;
    dd.put("viewName", "ViewDomainSystem");

    String startnode = (String) dd.getFormValue("start");
    if (startnode == null)
      startnode = "list";
    if ("list".equals(startnode))
      url = executeWithPermission("list", null);
    else if ("view".equals(startnode))
      url = this.executeWithPermission("view", null);
    else if ("viewOrganization".equals(startnode))
      url = this.executeWithPermission("viewOrganization", null);
    else if ("browse".equals(startnode))
      url = this.executeWithPermission("browse", null);

    return url;
  }

  protected void addForceFilter(BSQLSubquery bsq) throws BusinessException {
    SCE sce = new SCE(SCE.VALUE_OPERATOR_OR);

    if (getCurrentUser().hasPermission(PERMISSION_SELF_VIEW)) {
      SCE cond = SCE.cond("d.uuid = ?", getCurrentDomain().getUuid());
      sce.append(cond);
    }

    if (getCurrentUser().hasPermissions(
        PE.AND(PE.P(PERMISSION_LOWER_VIEW), PE.P(PERMISSION_LOWER_RECYCLE_VIEW)))) {
      SCE and = new SCE(SCE.VALUE_OPERATOR_AND);
      String levelId = getCurrentDomain().getLevelId();
      and.append(SCE.cond("d.levelId like ?", levelId, SCE.WC_RIGHT));
      SCE or = new SCE(SCE.VALUE_OPERATOR_OR);
      if (getCurrentUser().hasPermission(PERMISSION_LOWER_VIEW))
        or.append(SCE.cond("d.state != ?", CUser.VALUE_STATE_DELETED));
      if (getCurrentUser().hasPermission(PERMISSION_LOWER_RECYCLE_VIEW))
        or.append(SCE.cond("d.state = ?", CUser.VALUE_STATE_DELETED));
      and.append(or);
      sce.append(and);
    }

    SCE sceAnd = new SCE(SCE.VALUE_OPERATOR_AND);
    sceAnd.append(sce);
    sceAnd.append(SCE.cond("d.code != ?", CDomain.VALUE_ID_ROOT));
    String levelId = getCurrentDomain().getLevelId();
    sceAnd.append(SCE.cond("d.levelId like ?", levelId, SCE.WC_RIGHT));

    if (bsq.getWhereCondition() != null)
      bsq.setWhereCondition(SCE.and(bsq.getWhereCondition(), sceAnd));
    else
      bsq.setWhereCondition(sceAnd);
  }

  public String getPermission(String functionName, String domainUuid, Boolean isRecycle)
      throws BusinessException, HibernateException {
    BDomain domain = getBDomainMgr().loadDomain(domainUuid);
    int relation = getBDomainMgr().getRelativePosition(dd.getCurrentDomain(), domain);
    return getPermission(functionName, relation, isRecycle);
  }

  public String getPermission(String functionName, int relationship, Boolean isRecycle)
      throws BusinessException, HibernateException {
    if (relationship == IBDomainMgr.VALUE_RELATIONSHIP_SELF) {
      if (ViewDomain.FUNCTION_VIEW.equals(functionName))
        return PERMISSION_SELF_VIEW;
    } else if (relationship == IBDomainMgr.VALUE_RELATIONSHIP_LOWER) {
      if (ViewDomain.FUNCTION_VIEW.equals(functionName)) {
        if (isRecycle.booleanValue())
          return PERMISSION_LOWER_RECYCLE_VIEW;
        else
          return PERMISSION_LOWER_VIEW;
      }
    }
    return null;
  }

  protected void putFormPermissions(Boolean isRecycle) throws BusinessException {
    // TODO
  }

}
